//
//  Preferences.swift
//  Browser
//
//  Created by 薛永伟 on 2018/5/9.
//  Copyright © 2018年 114la.com. All rights reserved.
//

import UIKit
import SwiftyJSON

struct Preference {
    
    var rawValue:String
    
    /// 开发相关
    struct Develop{}
    
    /// 设置相关
    struct Setting{}
    
    /// APP相关
    struct App{}
    
    /// 用户相关
    struct User{}
    
    /// 通用相关
    struct Common {}
    
}

//MARK:- Preference分类
//MARK: 开发相关
extension Preference.Develop{
    
    /// API环境（测试、生产）
    static let APIEnvironment = Preference.init(rawValue: "DevelopAPIEnvironment")
}

//MARK: 设置相关
extension Preference.Setting {
    
    /// 消息：声音
    static let VoideClosed = Preference.init(rawValue: "SettingVoideClosed")
    /// 消息：震动
    static let ShockClosed = Preference.init(rawValue: "SettingShockClosed")
    
    /// 录制
    struct Recoder{
        
        /// 分辨率
        static let VideoSize = Preference.init(rawValue: "RecoderVideoSize")
        
        /// 码率
        static let VideoRate = Preference.init(rawValue: "RecoderVideoRate")
        
        /// 帧率
        static let VideoFrame = Preference.init(rawValue: "RecoderVideoFrame")
    }
}

//MARK: App相关
extension Preference.App {
    
    /// 是否完成版本引导
    static let HadVersionGuided = Preference.init(rawValue: "AppHadVersionGuided")
    /// 开屏图
    static let LaunchDiagram = Preference.init(rawValue: "AppLaunchDiagram")
}

//MARK: 用户相关
extension Preference.User{
    
    /// 用户信息
    static let Info = Preference.init(rawValue: "UserInfo")
    /// 授权token信息
    static let TokenAuth = Preference.init(rawValue: "UserTokenAuth")
    
}

//MARK: 通用相关
extension Preference.Common{
    
    /// 彩票颜色显示配置
    static let LotteryDisplayConfig = Preference.init(rawValue: "CommonLotteryDisplayConfig")
    
    
}


//MARK:  - Preference方法
extension Preference{
    
    var isTrue:Bool{
        return UserDefaults.standard.bool(forKey: self.rawValue)
    }
    
    var string:String?{
        return UserDefaults.standard.string(forKey: self.rawValue)
    }
    
    var int:Int{
        return UserDefaults.standard.integer(forKey: self.rawValue)
    }
    
    var dictionary:Dictionary<String, Any>?{
        return UserDefaults.standard.dictionary(forKey: self.rawValue)
    }
    
    var array:Array<Any>?{
        return UserDefaults.standard.array(forKey: self.rawValue)
    }
    
    var value:Any?{
        return UserDefaults.standard.value(forKey: self.rawValue)
    }
    /// JSON格式的数据
    var json:JSON?{
        if let data = self.value as? Data {
            do {
                let jsonData = try JSON.init(data: data)
                return jsonData
            }catch{
                return nil
            }
        }
        return nil
    }
    
    func setValue(any:Any?) {
        UserDefaults.standard.set(any, forKey: self.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    /// 缓存JSON数据
    func cacheJSON(_ data:JSON) {
        DispatchQueue.global().async {
            do {
                let cacheData = try data.rawData()
                self.setValue(any: cacheData)
            }catch{
                YRLog("未能将\(self.rawValue)数据缓存本地", type: .warning)
            }
        }
    }
    
}


